define(function(require, exports, module) {
	var template = require("template");
	var Ajax = require("ajax");
	var layer = require("layer");
	var Q = require("Q");
	//  require('jqueryUI');
	var home = function(callback) {
		this.init.apply(this, arguments);
	}

	home.prototype = {
		constructor: home,
		options: {},
		init: function(options) {
			var t = this;
			t.options = $.extend({}, t.options, options);
			$(".tipConNav").text("首页");
			$(window).unbind('scroll');
			//加载排序
			t.loadIndex();
			//滚动条置顶
			window.scrollTo(0,0);
			
		},
		loadIndex: function() {
			var t = this;
			//1,舆情预警 2,高危舆情 3,负面舆情 4,最新舆情
			var moduleA = 1;
			var moduleB = 2;
			var moduleC = 3;
			var moduleD = 4;
			var moudlePoint = '';
			var title = {};
			//加载舆情预警
			t.loadWarnInfo(moduleA, moduleB, moduleC, moduleD, moudlePoint, title);
			//加载高危舆情
			t.loadRiskInfo(moduleA, moduleB, moduleC, moduleD, moudlePoint, title);
			//加载负面舆情
			t.loadBadInfo(moduleA, moduleB, moduleC, moduleD, moudlePoint, title);
			//加载最新舆情
			t.loadNewInfo(moduleA, moduleB, moduleC, moduleD, moudlePoint, title);
		},

		loadWarnInfo: function(moduleA, moduleB, moduleC, moduleD, moudlePoint, title) {
			var t = this;
			title.titleName = "舆情预警";
			title.moreHref = "#!admin/home_warn";
			var index = 1;
			t.chargeModule(moduleA, moduleB, moduleC, moduleD, moudlePoint, title, index);
		},
		loadRiskInfo: function(moduleA, moduleB, moduleC, moduleD, moudlePoint, title) {
			var t = this;
			title.titleName = "高危舆情";
			title.moreHref = "#!admin/home_risk";
			var index = 2;
			t.chargeModule(moduleA, moduleB, moduleC, moduleD, moudlePoint, title, index);
		},
		loadBadInfo: function(moduleA, moduleB, moduleC, moduleD, moudlePoint, title) {
			var t = this;
			title.titleName = "负面舆情";
			title.moreHref = "#!admin/home_negative";
			var index = 3;
			t.chargeModule(moduleA, moduleB, moduleC, moduleD, moudlePoint, title, index);
		},
		loadNewInfo: function(moduleA, moduleB, moduleC, moduleD, moudlePoint, title) {
			var t = this;
			title.titleName = "最新舆情";
			title.moreHref = "#!admin/home_latest";
			var index = 4;
			t.chargeModule(moduleA, moduleB, moduleC, moduleD, moudlePoint, title, index);
		},
		chargeModule: function(moduleA, moduleB, moduleC, moduleD, moudlePoint, title, index) {
			var t = this;
			title.index = index;
			if(moduleA == index) {
				moudlePoint = 'A';
				title.pointNum = moudlePoint;
				$("#boxone").html(template("newInfoTemp", title));
			} else if(moduleB == index) {
				moudlePoint = 'B';
				title.pointNum = moudlePoint;
				
				$("#boxtwo").html(template("newInfoTemp", title));
			} else if(moduleC == index) {
				moudlePoint = 'C';
				title.pointNum = moudlePoint;
				$("#boxthree").html(template("newInfoTemp", title));
			} else if(moduleD == index) {
				moudlePoint = 'D';
				title.pointNum = moudlePoint;
				$("#boxfour").html(template("newInfoTemp", title));
			} 
			if(index == 1) {
				t.loadWarnInfoList(moudlePoint, index);
			} else if(index == 2) {
				t.loadRiskInfoList(moudlePoint, index);
			} else if(index == 3) {
				t.loadNegativeInfoList(moudlePoint, index);
			} else if(index == 4) {
				t.loadLatestInfoList(moudlePoint, index);
			} 
//			t.draggableFun();
		},

		loadWarnInfoList: function(moudlePoint, index) {
			var t = this;
			var timeScope =t.getDateStr(-6);
			Ajax.ajax({
				url: localhost + "Warning/findWarning.do",
				data: {
					greaterForward:20,
					timeScope:timeScope
				},
				beforeSend: function() {
					$("#geDom_" + moudlePoint).html(template("loadingDom"));
				},
				success: function(data) {
					if(data.returnData.length > 0) {
						var pointNum = moudlePoint;
						var item = data.returnData;
						t.switchCase(pointNum, item, index);
					} else {
						$("#geDom_" + moudlePoint).html(template("noWarnDataDom"));
					}
				}
			});
		},

		loadRiskInfoList: function(moudlePoint, index) {
			var t = this;
			var timeScope =t.getDateStr(-6);
			Ajax.ajax({
				url: localhost + "Warning/findRisk.do",
				data: {
					greaterForward:10,
					lessForward:20,
					timeScope:timeScope
				},
				beforeSend: function() {
					$("#geDom_" + moudlePoint).html(template("loadingDom"));
				},
				success: function(data) {
					if(data.returnData.length > 0) {
						var pointNum = moudlePoint;
						var item = data.returnData;
						t.switchCase(pointNum, item, index);
					} else {
						$("#geDom_" + moudlePoint).html(template("noRiskDataDom"));
					}
				}
			});
		},
		loadNegativeInfoList: function(moudlePoint, index) {
			var t = this;
			var timeScope =t.getDateStr(-6);
			Ajax.ajax({
				url: localhost + "Warning/findNegative.do",
				data:{
					lessForward:10,
					timeScope:timeScope
				},
				beforeSend: function() {
					$("#geDom_" + moudlePoint).html(template("loadingDom"));
				},
				success: function(data) {
					if(data.returnData.length > 0) {
						var pointNum = moudlePoint;
						var item = data.returnData;
						t.switchCase(pointNum, item, index);
					} else {
						$("#geDom_" + moudlePoint).html(template("noNegativeDataDom"));
					}
				}
			});

		},

		loadLatestInfoList: function(moudlePoint, index) {
			var t = this;
			var timeScope =t.getDateStr(-6);
			Ajax.ajax({
				url: localhost + "Warning/findNewest.do",
				data:{timeScope:timeScope},
				beforeSend: function() {
					$("#geDom_" + moudlePoint).html(template("loadingDom"));
				},
				success: function(data) {
					if(data.returnData.length > 0) {
						var pointNum = moudlePoint;
						var item = data.returnData;
						t.switchCase(pointNum, item, index);
					} else {
						$("#geDom_" + moudlePoint).html(template("noDataDom"));
					}
				}
			});
		},

		switchCase: function(pointNum, item, index) {
			var t = this;
			if(item.length > 5) {
				var length = 5;
			} else {
				var length = item.length;
			}
			$("#geDom_" + pointNum).html(template("infoUlTemp"));
			for(var i = 0; i < length; i++) {
				if(item[i].articleType == 'News') {
					item[i].sourceName = item[i].websiteName;
				}else if(item[i].articleType == 'Micro'){
					item[i].sourceName = "微博";
				}else if(item[i].articleType == 'BBS'){
					item[i].sourceName = "论坛";
				}else if(item[i].articleType == 'WeiXin'){
					item[i].sourceName = "微信";
				}
				
				item[i].releaseTime = item[i].releaseTime.substring(0,16);
				if(index==1){
					$("#geDom_" + pointNum).children('ul').append(template("warnLiTemp", item[i]));
				}else if(index==2){
					$("#geDom_" + pointNum).children('ul').append(template("riskLiTemp", item[i]));
				}else if(index==3){
					$("#geDom_" + pointNum).children('ul').append(template("negativeLiTemp", item[i]));
				}else if(index==4){
					$("#geDom_" + pointNum).children('ul').append(template("latestLiTemp", item[i]));
				}
				
			}
		},
		
		//获取7天前的时间
		getDateStr: function(AddDayCount){
			var curDate = new Date();
			curDate.setDate(curDate.getDate()+AddDayCount);
			var y = curDate.getFullYear();
			var m = curDate.getMonth()+1;
			var d = curDate.getDate();
			return y+'-'+m+'-'+d;
		}

	};
	module.exports = home;
});